package com.vcg.filter.service;

import com.alibaba.fastjson.JSONObject;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.vcg.filter.utils.SpringUtils;

/**
 * @author fyq on
 * @created by 2016/6/6
 */
public abstract class AbstractService implements Service {

    public DynamoDBMapper dynamoDBMapper;

    public AmazonDynamoDBClient dynamoDBClient;

    public final String dynamodbEndpoint = "dynamodb.ap-northeast-1.amazonaws.com";

    public AWSCredentialsProvider credentialsProvider;

    public AbstractService(){
        try {
            dynamoDBMapper = SpringUtils.getBean("dynamoDBMapper");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            dynamoDBClient = SpringUtils.getBean("dynamoDBClient");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected JSONObject getJSON(JSONObject jsonObject) {
        JSONObject json = new JSONObject();
        for(String key : jsonObject.keySet()) {
            String newKey = getKey(key);
            jsonObject.put(newKey, jsonObject.get(key));
        }
        return json;
    }

    protected String getKey(String key) {
        String[] keys = key.split("_");
        String jsonKey = null;
        for (int i = 0; i < keys.length; i++) {
            if (i == 0) {
                jsonKey = keys[i];
            } else {
                String first = keys[i].substring(0, 1).toUpperCase();
                String rest = keys[i].substring(1, keys[i].length()).toLowerCase();
                jsonKey += first + rest;
            }
        }
        return jsonKey;
    }

}
